Completed
Push — development ( 6352e5...8bea34 )
by Nils
07:55
created

install.js ➔ GotoNextStep   B

Complexity

Conditions 5
Paths 7

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 7
nop 0
dl 0
loc 42
rs 8.439
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A install.js ➔ ... ➔ $(ꞌ#admin_pwdꞌ).live(ꞌkeypressꞌ) 0 7 1
A install.js ➔ ... ➔ $(ꞌ#admin_pwdꞌ).live(ꞌpasteꞌ) 0 4 1
1
/**
2
 * @file          install.js
3
 * @author        Nils Laumaillé
4
 * @version       2.1.27
5
 * @copyright     (c) 2009-2011 Nils Laumaillé
6
 * @licensing     GNU AFFERO GPL 3.0
7
 * @link          http://www.teampass.net
8
 *
9
 * This library is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
 */
13
14
$(function() {
15
    $(".button").button();
16
    $("#but_launch, #step_error, #but_restart").hide();
17
18
    // no paste
19
    $('#admin_pwd').bind("paste",function(e) {
20
        alert('Paste option is disabled !!');
21
        e.preventDefault();
22
    });
23
});
24
25
function CheckPage()
26
{
27
    var step = $("#page_id").val();
28
    var data = "";
29
    var error = "";
30
    var index = "";
31
    var tasks = [];
32
    var multiple = "";
33
    var error_msg = "";
0 ignored issues
show
Unused Code introduced by
The variable error_msg seems to be never used. Consider removing it.
Loading history...
34
    $("#step_error").hide().html("");
35
    $("#res_"+step).html("");
36
37
    // STEP 2
38
    if (step == "2") {
39
        if ($("#url_path").val() == "" || $("#root_path").val() == "") {
40
            error = "Fields need to be filled in!";
41
        } else {
42
            data = '{"root_path":"'+$("#root_path").val()+'", "url_path":"'+$("#url_path").val()+'"}';
43
            tasks = ["folder*install", "folder*includes", "folder*files", "folder*upload", "extension*mcrypt", "extension*mbstring", "extension*openssl", "extension*bcmath", "extension*iconv", "extension*gd", "function*mysqli_fetch_all", "version*php", "ini*max_execution_time", "folder*includes/avatars", "extension*xml", "folder*includes/libraries/csrfp/libs", "folder*includes/libraries/csrfp/js", "folder*includes/libraries/csrfp/log", "folder*includes/config", "extension*curl"];
44
            multiple = true;
45
            $("#hid_abspath").val($("#root_path").val());
46
            $("#hid_url_path").val($("#url_path").val());
47
        }
48
    }
49
50
    // STEP 3
51
    if (step == "3") {
52
        if ($("#db_host").val() == "" || $("#db_db").val() == "" || $("#db_login").val() == "" || $("#db_port").val() == "") {
53
            error = "Paths need to be filled in!";
54
        } else if ($("#db_pw").val().indexOf('"') > -1) {
55
            error = "Double quotes in password not allowed!";
56
        } else {
57
            data = '{"db_host":"'+$("#db_host").val()+'", "db_bdd":"'+$("#db_bdd").val()+'", "db_login":"'+$("#db_login").val()+'", "db_pw":"'+$("#db_pw").val()+'", "db_port":"'+$("#db_port").val()+'", "abspath":"'+$("#hid_abspath").val()+'", "url_path":"'+$("#hid_url_path").val()+'"}';
58
            tasks = ["connection*test"];
59
            multiple = "";
60
            $("#hid_db_host").val($("#db_host").val());
61
            $("#hid_db_bdd").val($("#db_bdd").val());
62
            $("#hid_db_login").val($("#db_login").val());
63
            $("#hid_db_pwd").val($("#db_pw").val());
64
            $("#hid_db_port").val($("#db_port").val());
65
        }
66
    }
67
68
    // STEP 4
69
    if (step == "4") {
70
        if ($("#admin_pwd").val() == "") {
71
            error = "You must define a password for Admin account!";
72
        } else{
73
            data = '{"tbl_prefix":"'+sanitizeString($("#tbl_prefix").val())+'", "sk_path":"'+sanitizeString($("#sk_path").val())+'", "admin_pwd":"'+sanitizeString($("#admin_pwd").val())+'", "send_stats":""}';
74
            tasks = ["misc*preparation"];
75
            multiple = "";
76
        }
77
    }
78
79
    // STEP 5
80
    if (step == "5") {
81
        data = '';
82
        tasks = ["table*items", "table*log_items", "table*misc", "table*nested_tree", "table*rights", "table*users", "populate*admin", "table*tags", "table*log_system", "table*files", "table*cache", "table*roles_title", "table*roles_values", "table*kb", "table*kb_categories", "table*kb_items", "table*restriction_to_roles", "table*languages", "table*emails", "table*automatic_del", "table*items_edition", "table*categories", "table*categories_items", "table*categories_folders", "table*api", "table*otv", "table*suggestion", "table*tokens", "table*items_change"];
83
        multiple = true;
84
    }
85
86
    // STEP 6
87
    if (step == "6") {
88
        data = '{"url_path":"'+sanitizeString($("#hid_url_path").val())+'"}';
89
        tasks = ["file*settings.php", "file*sk.php", "file*security", "file*teampass-seckey", "file*csrfp-token"];
90
        multiple = true;
91
    }
92
93
    // STEP 7
94
    if (step == "7") {
95
        data = '';
96
        tasks = ["file*deleteInstall"];
97
        multiple = true;
98
    }
99
100
    // launch query
101
    if (error === "" && multiple === true) {
102
        $("#step_result").html("Please wait <img src=\"images/ajax-loader.gif\">");
103
        var globalResult = true;
0 ignored issues
show
Unused Code introduced by
The variable globalResult seems to be never used. Consider removing it.
Loading history...
104
        $("#step_res").val("true");
105
        $('#pop_db').html("");
106
        var ajaxReqs = [];
107
        for (index = 0; index < tasks.length; ++index) {
108
            var tsk = tasks[index].split("*");//console.log(tsk[0]+" - "+tsk[1]);
109
            ajaxReqs.push($.ajax({
110
                url: "install.queries.php",
111
                type : 'POST',
112
                dataType : "json",
113
                data : {
114
                    type:       "step_"+step,
115
                    data:       aes_encrypt(data), //
116
                    activity:   aes_encrypt(tsk[0]),
117
                    task:       aes_encrypt(tsk[1]),
118
                    db:         aes_encrypt('{"db_host" : "'+$("#hid_db_host").val()+'", "db_bdd" : "'+$("#hid_db_bdd").val()+'", "db_login" : "'+$("#hid_db_login").val()+'", "db_pw" : "'+$("#hid_db_pwd").val()+'", "db_port" : "'+$("#hid_db_port").val()+'"}'),
119
                    index:      index,
120
                    multiple:   multiple
121
                },
122
                complete : function(data, statut){
0 ignored issues
show
Unused Code introduced by
The parameter statut is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
123
                    if (data.responseText == "") {
124
                        // stop error occured, PHP5.5 installed?
125
                        $("#step_result").html("[ERROR] Answer from server is empty. This may occur if PHP version is not at least 5.5. Please check this this fit your server configuration!");
126
                    } else {
127
                        data = $.parseJSON(data.responseText);
128
                        if (data[0].error === "") {
129
                            if (step == "5") {
130
                                if (data[0].activity === "table") {
131
                                    $('#pop_db').append('<li>Table <b>'+data[0].task+'</b> created</li>');
132
                                } else if (data[0].activity === "entry") {
133
                                    $('#pop_db').append('<li>Entries <b>'+data[0].task+'</b> were added</li>');
134
                                }
135
                            } else {
136
                                $("#res"+step+"_check"+data[0].index).html("<img src=\"images/tick.png\">");
137
                            }
138
139
                            if (data[0].result != undefined && data[0].result != "" ) {
140
                                $("#step_result").html(data[0].result);
141
                            }
142
                        } else {
143
                            // ignore setting error if regarding setting permissions (step 6, index 2)
144
                            if (step+data[0].index != "62") {
145
                                $("#step_res").val("false");
146
                            }
147
                            $("#res"+step+"_check"+data[0].index).html("<img src=\"images/exclamation-red.png\">&nbsp;<i>"+data[0].error+"</i>");
148
                            $('#pop_db').append('<li><img src=\"images/exclamation-red.png\">&nbsp;Error on task `<b>'+data[0].activity+' > '+data[0].task+'`</b>. <i>'+data[0].error+'</i></li>');
149
                            if (data[0].result != undefined && data[0].result != "" ) {
150
                                $("#step_result").html(data[0].result);
151
                            }
152
                        }
153
                    }
154
                }
155
            }));
156
        }
157
        $.when.apply($, ajaxReqs).done(function(data, statut) {
0 ignored issues
show
Unused Code introduced by
The parameter statut is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
158
            setTimeout(function(){
159
                // all requests are complete
160
                if ($("#step_res").val() == "false") {
161
                    data = $.parseJSON(data.responseText);
162
                    $("#step_error").show().html("At least one task has failed! Please correct and relaunch. ");
163
                    $("#res_"+step).html("<img src=\"images/exclamation-red.png\">");
164
                } else {
165
                    $("#but_launch").prop("disabled", true);
166
                    $("#but_launch").hide();
167
                    $("#but_next").prop("disabled", false);
168
                    $("#but_next").show();
169
                    // Hide restart button at end of step 6 if successful
170
                    if (step == "7") {
171
                        $("#but_restart").prop("disabled", true);
172
                        $("#but_restart").hide();
173
                    }
174
                }
175
                $("#step_result").html("");
176
            }, 1000);
177
        });
178
    } else if (error == "" && multiple == "") {
179
        $("#step_result").html("Please wait <img src=\"images/ajax-loader.gif\">");
180
        var tsk = tasks[0].split("*");
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable tsk already seems to be declared on line 108. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
181
        $.ajax({
182
            url: "install.queries.php",
183
            type : 'POST',
184
            dataType : "json",
185
            data : {
186
                type:       "step_"+step,
187
                data:       aes_encrypt(data),
188
                activity:   aes_encrypt(tsk[0]),
189
                task:       aes_encrypt(tsk[1]),
190
                db:         aes_encrypt('{"db_host" : "'+$("#hid_db_host").val()+'", "db_bdd" : "'+$("#hid_db_bdd").val()+'", "db_login" : "'+$("#hid_db_login").val()+'", "db_pw" : "'+$("#hid_db_pwd").val()+'", "db_port" : "'+$("#hid_db_port").val()+'"}'),
191
                index:      index,
192
                multiple:   multiple
193
            },
194
            complete : function(data, statut){
0 ignored issues
show
Unused Code introduced by
The parameter statut is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
195
                data = $.parseJSON(data.responseText);
196
                $("#step_result").html("");
197
                if (data[0].error != "" ) {
198
                    $("#step_error").show().html("The next ERROR occurred: <i>'"+data[0].error+"'</i><br />Please correct and relaunch.");
199
                    $("#res_"+step).html("<img src=\"images/exclamation-red.png\">");
200
                } else {
201
                    if (data[0].result != undefined && data[0].result != "" ) {
202
                        $("#step_result").html("<span style=\"font-weight:bold; margin-right:20px;\">"+data[0].result+"</span>");
203
                    }
204
                    $("#but_launch").prop("disabled", true);
205
                    $("#but_launch").hide();
206
                    $("#but_next").prop("disabled", false);
207
                    $("#but_next").show();
208
                }
209
            },
210
            error : function(resultat, statut, erreur){
0 ignored issues
show
Unused Code introduced by
The parameter resultat is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter statut is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter erreur is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
211
212
            }
213
        });
214
    } else {
215
        $("#step_error").show().html(error);
216
    }
217
}
218
219
220
function GotoNextStep()
221
{
222
    var step = $("#page_id").val();
223
    var nextStep = parseInt(step)+1;
224
225
    if (nextStep == 8) {
226
        $("#but_restart, #but_next, #but_launch").hide();
227
        $("#but_start").show();
228
        $("#step_result").html("Installation finished.");
229
        $("#step_name").html($("#menu_step"+nextStep).html());
230
        $("#step_content").html($("#text_step"+nextStep).html());
231
        $("#menu_step"+step).switchClass("li_inprogress", "li_done");
232
        $("#menu_step"+nextStep).switchClass("", "li_inprogress");
233
        $("#res_"+step).html("<img src=\"images/tick.png\">");
234
    } else {
235
        $("#page_id").val(nextStep);
236
        $("#but_launch").show().prop("disabled", false);
237
        $("#but_launch").show();
238
        $("#but_restart").show();
239
        $("#but_next").prop("disabled", true);
240
        $("#but_next").hide();
241
        $("#menu_step"+step).switchClass("li_inprogress", "li_done");
242
        $("#menu_step"+nextStep).switchClass("", "li_inprogress");
243
        $("#res_"+step).html("<img src=\"images/tick.png\">");
244
        $("#step_result").html("");
245
        $("#step_name").html($("#menu_step"+nextStep).html());
246
        $("#step_content").html($("#text_step"+nextStep).html());
247
        $('#admin_pwd').live("paste",function(e) {
248
            alert('Paste option is disabled !!');
249
            e.preventDefault();
250
        });
251
        $("#admin_pwd").live('keypress', function(e){
252
            var key = e.charCode || e.keyCode || 0;
253
            // allow backspace, tab, delete, arrows, letters, numbers and keypad numbers ONLY
254
            return (
255
                key != 39
256
            );
257
        });
258
        // Auto start as required
259
        if (nextStep == 5 || nextStep == 6 || nextStep ==7 ) CheckPage();
260
    }
261
}
262
263
function aes_encrypt(text)
264
{
265
    return Aes.Ctr.encrypt(text, "cpm", 128);
266
}
267